<%@page import="com.scm.model.ScmUser"%>
<%@page import="com.scm.util.*"%>
<%@page import="com.scm.util.PomainIdUtil"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="../css/style.css" rel="stylesheet" type="text/css">
<script language="javascript" src="../script/common.js" ></script>
<script type="text/javascript" src="../script/jquery-1.8.1.min.js" ></script>
<script type="text/javascript">
	function addItem() {
		var detailTable = document.getElementById("detailTable");
		var oRow = detailTable.insertRow(-1);//在表格最后添加一行
		oRow.align = "center";
		oRow.className = "toolbar";
		oCell = oRow.insertCell(0);//添加单元格
		oCell.innerHTML = "<input type='text'  name='productCode' size='10' onClick='showProduct(this)' readonly required>";
		oCell = oRow.insertCell(1);
		oCell.innerHTML = "<input type='text'  name='name' size='10' readonly required>";
		oCell = oRow.insertCell(2);
		oCell.innerHTML = "<input type='text' name='unitName' size='15' readonly required>";
		oCell = oRow.insertCell(3);
		oCell.innerHTML = "<input type='text' name='num' size='10' value='0' onchange='sum()'>";
		oCell = oRow.insertCell(4);
		oCell.innerHTML = "<input type='text' name='unitPrice' size='10' value='0' onchange='sum()'>";
		oCell = oRow.insertCell(5);
		oCell.innerHTML = "<a class=\"LL\" onclick=\"delItem(" + oRow.rowIndex + ")\">删除</a>";
	}
	function check(){
		var productCode=$("[name=productCode]").val();
		var venderName=$("[name=venderName]").val();
		var productTotal = document.getElementsByName("productTotal");
		var prePayFee = document.getElementsByName("prePayFee");
		var nums = document.getElementsByName("num");
		var tab = document.getElementById("detailTable");
		if(venderName==''){
			alert("请选择供应商");
			return false;
		}
		if(productCode==''){
			alert("请选择产品");
			return false;
		}
		for(var i=0;i<nums.length;i++){
			if(nums[i].value=='0'){
				alert("请输入明细产品数目");
				return false;
			}
		}
		if(prePayFee[0].value>productTotal[0].value){
			alert("预付款金额不能大于总价！");
			return false;
		}else if(tab.rows.length==1){
			alert("请添加明细信息");
			return false;
		}
	}
	function sum(){
		var sum=0;
		var tab = document.getElementById("detailTable");
		for(var i=1;i<tab.rows.length;i++){
			sum+=tab.rows[i].cells[3].childNodes[0].value*tab.rows[i].cells[4].childNodes[0].value;
		}
		var productTotal = document.getElementsByName("productTotal");
		productTotal[0].value=sum;
	}
	//删除行,注意这里的行号全部要重新计算 刷新的
	function delItem(index) {
		var detailTable = document.getElementById("detailTable");
		detailTable.deleteRow(index);
		var rowNum = detailTable.rows.length;
		var rowlength = detailTable.rows[0].cells.length;
		for (i = index; i < rowNum; i++) {
			detailTable.rows[i].cells[0].innerHTML = i;
			detailTable.rows[i].cells[rowlength - 1].innerHTML = "<image src=\"../images/delete.gif\" class=\"LL\" onclick=\"delItem(" + i + ")\"/>";
		}
		sum();
	}
	var finput;
	function showProduct(obj){
		currentPagePr=1;
		document.getElementById("product").style.display="block";
		document.getElementById("main").style.display="none";
		finput = obj;
		queryProduct();
	}
	var currentPagePr=1;
	function queryProduct(){
		$.ajax({
			type:"post",
			url:"../main/somain/ListAllProduct",
			data:{currentPagePr:currentPagePr},
			dataType:"json",
			success:function(msg){
				$("#tproduct").children().remove();
				for(var i=0;i<msg.dataList.length;i++){ 
					$("#tproduct").append("<tr align='center'><td>"+msg.dataList[i].productCode+"</td><td>"
							+msg.dataList[i].name+"</td><td>"+msg.dataList[i].unitName+"</td><td hidden>"+msg.dataList[i].price+"</td><td><input type='button' value='选择' onclick='chooseProduct(this)'/></td></tr>");
				}
				$("#pc").text(msg.currentPage);
				$("#pa").text(msg.pageCount);
			}
		});
	}
	function chooseProduct(btn){
		var rows = finput.parentNode.parentNode.parentNode.rows;
		for(var i=1;i<rows.length;i++){
			if(rows[i].cells[0].childNodes[0].value==btn.parentNode.parentNode.cells[0].innerHTML){
				alert("不可重复添加！");
				return;
			}
		}
		finput.value=btn.parentNode.parentNode.cells[0].innerHTML;
		finput.parentNode.parentNode.cells[1].childNodes[0].value=btn.parentNode.parentNode.cells[1].innerHTML;
		finput.parentNode.parentNode.cells[2].childNodes[0].value=btn.parentNode.parentNode.cells[2].innerHTML;
		finput.parentNode.parentNode.cells[4].childNodes[0].value=btn.parentNode.parentNode.cells[3].innerHTML;
				
		document.getElementById("product").style.display="none";
		document.getElementById("main").style.display="block";
		sum();	
	}
	function showVender(){
		document.getElementById("vender").style.display="block";
		document.getElementById("main").style.display="none";
		queryVender();
	}
	var currentPage=1;
	function queryVender(){
		$.ajax({
			type:"post",
			url:"../main/pomain/ListAllVender",
			data:{currentPage:currentPage},
			dataType:"json",
			success:function(msg){
				$("#tvender").children().remove();
				for(var i=0;i<msg.dataList.length;i++){ 
					$("#tvender").append("<tr align='center'><td>"+msg.dataList[i].venderCode+"</td><td>"
							+msg.dataList[i].name+"</td><td><input type='button' value='选择' onclick='chooseVender(this)'/></td></tr>");
				}
				$("#vc").text(msg.currentPage);
				$("#va").text(msg.pageCount);
			}
		});
	}
	function chooseVender(obj){
		$("[name=venderName]").val(obj.parentNode.parentNode.cells[1].innerHTML);
		$("[name=venderCode]").val(obj.parentNode.parentNode.cells[0].innerHTML);
		document.getElementById("vender").style.display="none";
		document.getElementById("main").style.display="block";
	}
	function vendergoPage(obj){
		if(obj=='first'){
			currentPage=1;
			queryVender();
		}else if(obj=='front'){
			if(currentPage==1){
				alert("已经是首页了");
			}else{
				currentPage-=1;
				queryVender();
			} 
		}else if(obj=='next'){
			if(currentPage==$("#ap").text()){
				alert("已经是末页了");
			}else{
				currentPage+=1;
				queryVender();
			}
		}else{
			if(currentPage+1<=$("#ap").text()){
				currentPage=$("#ap").text();
				queryVender();
			}
		}
	}
	function productgoPage(obj){
		if(obj=='first'){
			currentPagePr=1;
			queryProduct();
		}else if(obj=='front'){
			if(currentPagePr==1){
				alert("已经是首页了");
			}else{
				currentPagePr-=1;
				queryProduct();
			} 
		}else if(obj=='next'){
			if(currentPagePr==$("#pa").text()){
				alert("已经是末页了");
			}else{
				currentPagePr+=1;
				queryProduct();
			}
		}else{
			if(currentPage+1<=$("#pa").text()){
				currentPage=$("#pa").text();
				queryProduct();
			}
		}
	}
	function payTypeChange(){
		if($("[name=payType]").val()==("预付款到发货")){
			$("[name=prePayFee]").attr("readonly",false);
		}
	}
	function goback(){
		document.getElementById("vender").style.display="none";
		document.getElementById("main").style.display="block";
		document.getElementById("product").style.display="none";
	}
	function gobackpage(){
		window.history.back();
	}
</script>

</head>
<body onLoad="init()">
<div id="main">
<table width="100%"  border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td nowrap class="title1">您的位置：工作台面－－采购管理－－新增采购单</td>
  </tr>
</table>
<div class="formVisiblitly" id="formDiv"></div>
<form action="../main/pomain/AddPomain" name="mainFrm">
<table width="100%"  border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="30px" nowrap class="toolbar"><span style="color:red">采购单主信息：</span></td>
	<td nowrap class="toolbar">&nbsp;</td>
  </tr>
</table>

<table id="headTable" width="100%"  border="0" align="center" class="a1">
  <tr align="justify">
    <td>采购单编号</td>
    <td><input type="text" name="poId" value="<%=PomainIdUtil.PomainId() %>" size="15" readonly/>
      <span class="requred_symbol">*</span></td>
    <td>创建时间</td>
    <td><input type="text" size="15" name="createTime" value="<%=DateUtil.currentDate()%>" readonly/>
      <span class="requred_symbol">*</span></td>
    <td>供应商的名称</td>
    <td><input type="text" size="15" name="venderName" onclick="showVender()"/><input type="text" name="venderCode" hidden/>
      <span class="requred_symbol">*</span>
      </td>
    <td>创建用户</td>
    <td><input type="text" size="15" name="name" readonly value="${user.name}"/><input type="text" name="account" hidden value="${user.account }"/>
        <span class="requred_symbol">*</span>
    </td>
  </tr>
  <tr align="justify">
    <td>附加费用</td>
    <td><input type="text" size="15" value="0" name="tipFee" />
        <span class="requred_symbol">*</span></td>
    <td>采购产品总价</td>
    <td><input type="text" size="15" name="productTotal" value='0' readonly/></td>
    <td>付款方式</td>
    <td><select name="payType" onchange="payTypeChange()"><option>货到付款</option><option>款到发货</option><option>预付款到发货</option></select></td>
    <td>备注</td>
    <td><input type="text" size="15" name="remark"/></td>
   </tr>
  <tr align="justify">
    <td>最低预付款金额</td>
    <td><input type="text" size="15" name="prePayFee" value=0 readonly/></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
<table width="100%"  border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="30px" nowrap class="toolbar"><span style="color:red">采购单明细：</span></td>
	<td nowrap class="toolbar">&nbsp;</td>
  </tr>
</table>
<table width="100%"  border="0" align="center" cellspacing="1" id="detailTable">
  <tr>
    <td class="toolbar"> 产品编号 </td>
    <td class="toolbar"> 产品名称 </td>
    <td class="toolbar"> 数量单位 </td>
    <td class="toolbar"> 产品数量 </td>
    <td class="toolbar"> 采购单价 </td>
    <td class="toolbar"> 操作 </td>
  </tr>
</table>
<table width="100%"  border="0" align="center" cellspacing="1">
  <tr>
    <td class="title2"></td>
  </tr>
</table>
<br/>
<div align="center">
    <input type="button" id="mx" value="增加明细" onClick="addItem()"/>
    <input type="submit" id="bc" value="保存" onclick="return check()"/>
    <input name="reset"  id="cz" value="重置" type="reset"/>
     <input type="button" value="返回" onclick="gobackpage()" />
</div>
</form>
</div>
<div hidden id="vender">
	<table width="100%"  border="0" align="center" cellspacing="1" id="venderTable">
	  <tr>
	    <td class="toolbar"> 供应商编号 </td>
	    <td class="toolbar"> 供应商名称 </td>
	    <td class="toolbar"> 操作 </td>
	  </tr>
	  <tbody id="tvender">
	  	
	  </tbody>
	</table>
	<center><br/>
	<input type="button" value="首页" onclick="vendergoPage('first')"/>
	<input type="button" value="上一页" onclick="vendergoPage('front')" />
	<input type="button" value="下一页" onclick="vendergoPage('next')"/>
	<input type="button" value="末页" onclick="vendergoPage('end')"/>
	当前第<span id="vc"></span>页
    	共<span id="va"></span>页
    <input type="button" value="返回" onclick="goback()"/>
	</center>
</div>
<div hidden id="product">
	<table width="100%"  border="0" align="center" cellspacing="1" id="productTable">
	  <tr>
	    <td class="toolbar"> 产品编号 </td>
	    <td class="toolbar"> 产品名称 </td>
	    <td class="toolbar"> 数量单位 </td>
	    <td class="toolbar"> 操作 </td>
	  </tr>
	  <tbody id="tproduct">
	  	
	  </tbody>
	</table><br/>
	<center>
	<input type="button" value="首页" onclick="productgoPage('first')"/>
	<input type="button" value="上一页" onclick="productgoPage('front')" />
	<input type="button" value="下一页" onclick="productgoPage('next')"/>
	<input type="button" value="末页" onclick="productgoPage('end')"/>
	当前第<span id="pc"></span>页
    	共<span id="pa"></span>页
    <input type="button" value="返回" onclick="goback()"/>
	</center>
</div>
</body>
</html>